package com.hj.saapp.control.activity;

import android.text.TextUtils;
import android.view.View;
import android.widget.RadioButton;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.hj.saapp.BCDApplication;
import com.hj.saapp.BuildConfig;
import com.hj.saapp.R;
import com.hj.saapp.common.utils.PreferencesUtils;
import com.hj.saapp.control.activity.base.BaseActivity;
import com.hj.saapp.view.CleanEditText;

import static com.hj.saapp.BCDApplication.isDevType;
import static com.hj.saapp.BCDApplication.printStr;


public class SetingActivity extends BaseActivity implements View.OnClickListener{


    private RelativeLayout activityLogin;
    private CleanEditText longinUrlTv;
    private CleanEditText businessUrlTv;
    private CleanEditText ptintTv;//打印
    private TextView saveBtn;//保存按钮
    private RadioButton type1;//手持机类型
    private RadioButton type2;

    private void assignViews() {
        //获取服务器接口(接入区)
        String loginUrl = PreferencesUtils.getInstance().getString(BCDApplication.getApp(), "loginUrl", BuildConfig.HOST);//
        //获取服务器接口（接受区）
        String businessUrl = PreferencesUtils.getInstance().getString(BCDApplication.getApp(), "businessUrl", BuildConfig.HOST);
        activityLogin = (RelativeLayout) findViewById(R.id.activity_login);
        saveBtn = (TextView) findViewById(R.id.saveBtn);

        longinUrlTv = (CleanEditText) findViewById(R.id.longinUrlTv);
        businessUrlTv = (CleanEditText) findViewById(R.id.businessUrlTv);
        ptintTv = (CleanEditText) findViewById(R.id.ptintTv);
        type1 = (RadioButton) findViewById(R.id.type1);
        type2 = (RadioButton) findViewById(R.id.type2);
        longinUrlTv.setText(loginUrl);
        businessUrlTv.setText(businessUrl);
        saveBtn.setOnClickListener(this);

        if (!TextUtils.isEmpty(printStr)) {
            ptintTv.setText(printStr);
        }
        if (!isDevType) {
            type1.setChecked(true);
        } else {
            type2.setChecked(true);

        }

    }

    @Override
    public void initView() {
        assignViews();



    }

    @Override
    public int getTitleById() {
        return R.string.srting_title;
    }

    @Override
    public int getLayoutByid() {
        return R.layout.activity_seting;
    }



    @Override
    public void onClick(View v) {

        switch (v.getId()) {

            case R.id.saveBtn:
                showPopDialog("保存成功");

                if (!TextUtils.isEmpty(longinUrlTv.getText())) {

                    PreferencesUtils.getInstance().putString(BCDApplication.getApp(),"loginUrl", longinUrlTv.getText().toString());
                }
                else
                {
                    PreferencesUtils.getInstance().putString(BCDApplication.getApp(),"loginUrl", BuildConfig.HOST);

                }
                if (!TextUtils.isEmpty(businessUrlTv.getText()))
                {

                    PreferencesUtils.getInstance().putString(BCDApplication.getApp(),"businessUrl", businessUrlTv.getText().toString());
                    BCDApplication.Service_addr = businessUrlTv.getText().toString();

                }
                else
                {
                    PreferencesUtils.getInstance().putString(BCDApplication.getApp(),"businessUrl", BuildConfig.HOST);
                    BCDApplication.Service_addr = BuildConfig.HOST;

                }

                if (!TextUtils.isEmpty(ptintTv.getText())) {
                    PreferencesUtils.getInstance().putString(BCDApplication.getApp(),"ptintStr", ptintTv.getText().toString());
                    printStr = ptintTv.getText().toString();
                }
                if (type1.isChecked()) {
                    PreferencesUtils.getInstance().putBoolean(BCDApplication.getApp(), "isDevType", false);
                    BCDApplication.isDevType = false;
                } else {
                    PreferencesUtils.getInstance().putBoolean(BCDApplication.getApp(), "isDevType", true);
                    BCDApplication.isDevType = true;
                }
                break;
            default:
                this.finish();

                break;

        }


    }
}
